﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLL;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web
{
    public partial class SelectActualPrice : System.Web.UI.Page
    {
        BLL_HospitalProcureCatalog com = new BLL_HospitalProcureCatalog();
        public static string ProjectID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            ProjectID = Request.QueryString["ProjectID"];
            GetDefaultAreaString();
            if (!IsPostBack)
            {
                Bind();
            }
        }
        /// <summary>
        /// 绑定数据源
        /// </summary>
        public void Bind()
        {
            int totalRecord = 0;
            HZ.DrugTrade.Model.Query.HospitalProcureCatalog hp = new Model.Query.HospitalProcureCatalog();
            if (Request.QueryString["goodsid"] != null)
            {
                hp.ProcureCatalogID = Request.QueryString["goodsid"];
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 2).Equals("QH"))
            {
                hp.HospitalID = CurrentAccount.GetUser().UserID;
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 2).Equals("QP"))
            {
                hp.Companyid_Ps = CurrentAccount.GetUser().UserID;
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 2).Equals("QS"))
            {
                hp.Companyid_SC = CurrentAccount.GetUser().UserID;
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 2).Equals("QJ"))
            {
                hp.Companyid_Tb = CurrentAccount.GetUser().UserID;
            }

            hp.Append = string.Format(" Price is not null ");
            if (!string.IsNullOrEmpty(GetAreaID()))
            {
                if (GetAreaID().Substring(2, 4).Equals("0000")) //代表省级别
                {
                    hp.Append += " and AreaID LIKE '" + GetAreaID().Substring(0, 2) + "%'";
                }
                else if (GetAreaID().Substring(4, 2).Equals("00")) //末尾是“00”代表市级
                {
                    hp.Append += " and  AreaID LIKE '" + GetAreaID().Substring(0, 4) + "%'";
                }
                else
                {
                    hp.Append += " and  AreaID = '" + GetAreaID() + "'";
                }
            }

            hp.Companyname_Ps = txtCmpName.Text.Trim();
            hp.HospitalName = txtHosName.Text.Trim();
            IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> hplist = com.GetHospitalProcureCatalog(hp, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);
            AspNetPager1.RecordCount = totalRecord;
            gvwShowDate.DataSource = hplist;
            gvwShowDate.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int totalRecord = 0;
            HZ.DrugTrade.Model.Query.HospitalProcureCatalog hp = new Model.Query.HospitalProcureCatalog();
            if (Request.QueryString["goodsid"] != null)
            {
                hp.ProcureCatalogID = Request.QueryString["goodsid"];
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 2).Equals("QH"))
            {
                hp.HospitalID = CurrentAccount.GetUser().UserID;
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 2).Equals("QP"))
            {
                hp.Companyid_Ps = CurrentAccount.GetUser().UserID;
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 2).Equals("QS"))
            {
                hp.Companyid_SC = CurrentAccount.GetUser().UserID;
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 2).Equals("QJ"))
            {
                hp.Companyid_Tb = CurrentAccount.GetUser().UserID;
            }
            if (!string.IsNullOrEmpty(GetAreaID()))
            {
                if (GetAreaID().Substring(2, 4).Equals("0000")) //代表省级别
                {
                    hp.Append = " AreaID LIKE '" + GetAreaID().Substring(0, 2) + "%'";
                }
                else if (GetAreaID().Substring(4, 2).Equals("00")) //末尾是“00”代表市级
                {
                    hp.Append = "  AreaID LIKE '" + GetAreaID().Substring(0, 4) + "%'";
                }
                else
                {
                    hp.Append = "  AreaID = '" + GetAreaID() + "'";
                }
            }
            hp.Companyname_Ps = txtCmpName.Text.Trim();
            hp.HospitalName = txtHosName.Text.Trim();
            IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> hplist = com.GetHospitalProcureCatalog(hp, 1, 0, out totalRecord);
            System.Data.DataTable tb = HZ.Data.ModelConvert.GetDataTableFromModel(hplist);
            HZ.Excel.ExcelHelper.ExportDataForWeb(tb, gvwShowDate);
        }
        #region 获取地区信息
        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用当前用户所在区域ID
            areaId = CurrentAccount.GetUser().Org.Area.AreaID;

            //优先获取区县，其次城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"];
            }
            else
            {
                if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
                }
                else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
                }

            }
            return areaId;
        }

        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "150000";
            string defaultCity = "";
            //
            string strArea = "";

            //获取当前用户所在区域
            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(CurrentAccount.GetUser().Org.Area.AreaID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            //显示默认地区，范围之外的内容禁止选择
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }
        #endregion

        protected void gvwShowDate_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (ProjectID == "0002")
                {
                    e.Row.Cells[e.Row.Cells.Count - 2].Text = "自主报价";
                }
                else
                {
                    e.Row.Cells[e.Row.Cells.Count - 2].Text = "采购限价";
                }
            }
        }
    }
}